#include "deviceCodeResMessage.h"
#include <rapidjson/document.h>
#include "jsonUtils.h"

DeviceCodeResMessage::DeviceCodeResMessage(MtpProtocol* packet) : MtpMessage(packet)
{

}

DeviceCodeResMessage::DeviceCodeResMessage(const Char* deviceCode,MtpMessage* src) 
	: MtpMessage(new MtpProtocol(L"deviceCodeRes",(MtpProtocol*)src->getPacket())),deviceCode(deviceCode),res(true)
{

}

void DeviceCodeResMessage::decodeBody()
{
	MtpMessage::decodeBody();
	if (!jsonContent.empty()) {
		JsonDoc& doc = res.jsonDeserialize(jsonContent);
		const JsonValue::ConstMemberIterator& it = doc.FindMember(L"data");
		if (it != doc.MemberEnd() && it->value.IsObject()) {
			JsonUtils::loadStr(it->value, L"code", deviceCode);
		}
	}
}

void DeviceCodeResMessage::encodeBody()
{
	JsonDoc& doc = res.jsonSerialize();
	JsonValue v(rapidjson::kObjectType);
	JsonUtils::addString(v, L"code", deviceCode, doc.GetAllocator());
	doc.AddMember(L"data", v, doc.GetAllocator());
	jsonContent = JsonUtils::jsonStr(doc);
	MtpMessage::encodeBody();
}
